<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>有效括号</title>
  </head>
  <body>
    <script>
      var s = "{{{{}}}}";
      //1.运用栈的思想：后入先出;
      //2.遍历字符串，当为左括号，在数组中添加与其对应的右括号;
      //             当为右括号，用pop()函数删除并获取数组最后一个元素并与右括号比较
      //3.若能通过遍历字符串，则返回数组长度的Boolean值(判断是否有多余的左括号)
      function isValid(s) {
        var arr = [];
        for (var i = 0; i < s.length; i++) {
          if (s[i] == "{") {
            arr.push("}");
          } else if (s[i] === "[") {
            arr.push("]");
          } else if (s[i] === "(") {
            arr.push(")");
          } else if (arr.pop() !== s[i]) {
            return false;
          }
        }
        return !arr.length;
      }
      var e = isValid(s);
      console.log(e);
    </script>
  </body>
</html>
